/*     */ package com.yc.upload;
/*     */ 
/*     */ import com.yc.dao.FileDAO;
/*     */ import com.yc.dao.model.FileBO;
/*     */ import com.yc.service.FolderService;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.SocketException;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import java.util.StringTokenizer;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ 
/*     */ public class UploadCommServlet extends HttpServlet
/*     */ {
/*  35 */   private static final Log log = LogFactory.getLog(UploadCommServlet.class);
/*  36 */   private String tempPath = "C:\\upload\\tmp";
/*  37 */   private String uploadCommPath = "";
/*  38 */   private File tempPathFile = null;
/*  39 */   private FileDAO fileDao = null;
/*  40 */   private String propertiesFileName = "/btsm.properties";
/*     */ 
/*     */   public void init()
/*     */   {
/*  44 */     this.fileDao = new FileDAO();
/*     */     try
/*     */     {
/*  47 */       Properties p = new Properties();
/*  48 */       InputStream in = FolderService.class.getResourceAsStream(this.propertiesFileName);
/*  49 */       p.load(in);
/*  50 */       in.close();
/*  51 */       if (p.containsKey("uploadCommPath"))
/*  52 */         this.uploadCommPath = p.getProperty("uploadCommPath");
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*  56 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void createTempFolder()
/*     */   {
/*  62 */     this.tempPathFile = new File(this.tempPath);
/*  63 */     boolean bFile = this.tempPathFile.exists();
/*     */ 
/*  65 */     if (bFile) {
/*  66 */       log.info("The folder exists.");
/*     */     } else {
/*  68 */       log.info("The folder do not exist,now trying to create a one...");
/*  69 */       bFile = this.tempPathFile.mkdirs();
/*  70 */       if (bFile)
/*  71 */         log.info("Create successfully!");
/*     */       else
/*  73 */         log.info("Disable to make the folder,please check the disk is full or not.");
/*     */     }
/*     */   }
/*     */ 
/*     */   private String getFilepPathForDatabase(String strFolderID)
/*     */   {
/*  80 */     String strFilePath = this.fileDao.getFolderPath(strFolderID);
/*  81 */     StringTokenizer token = new StringTokenizer(strFilePath, ",");
/*  82 */     StringBuffer filePathBuffer = new StringBuffer();
/*  83 */     while (token.hasMoreTokens())
/*     */     {
/*  85 */       filePathBuffer.append(token.nextToken());
/*  86 */       filePathBuffer.append("\\\\");
/*     */     }
/*  88 */     return filePathBuffer.toString();
/*     */   }
/*     */ 
/*     */   private Map getShareFolderPath()
/*     */   {
/*  94 */     Map map = new HashMap();
/*  95 */     StringBuffer buff = new StringBuffer();
/*  96 */     StringBuffer buffData = new StringBuffer();
/*  97 */     buff.append(this.uploadCommPath);
/*  98 */     StringTokenizer token = new StringTokenizer(this.uploadCommPath, "\\");
/*  99 */     while (token.hasMoreTokens())
/*     */     {
/* 101 */       buffData.append(token.nextToken());
/* 102 */       buffData.append("\\\\");
/*     */     }
/* 104 */     map.put("DATA", buffData.toString());
/* 105 */     map.put("PATH", buff.toString());
/* 106 */     return map;
/*     */   }
/*     */ 
/*     */   private String getFilepPath(String strFolderID)
/*     */   {
/* 112 */     return this.fileDao.getFolderPath(strFolderID);
/*     */   }
/*     */ 
/*     */   private void saveFile(String strFileName, String strFolderID, String strUserID, String strFilePath, long size)
/*     */   {
/* 118 */     StringTokenizer token = new StringTokenizer(strFilePath, "\\");
/* 119 */     StringBuffer filePathBuffer = new StringBuffer();
/*     */ 
/* 121 */     int count = token.countTokens();
/* 122 */     int i = 0;
/* 123 */     while (token.hasMoreTokens())
/*     */     {
/* 125 */       filePathBuffer.append(token.nextToken());
/* 126 */       if (i < count - 1)
/* 127 */         filePathBuffer.append("\\\\");
/* 128 */       i++;
/*     */     }
/*     */ 
/* 131 */     FileBO fileBo = new FileBO();
/* 132 */     fileBo.setFileId(this.fileDao.queryMaxFileID());
/* 133 */     fileBo.setFileName(strFileName);
/* 134 */     fileBo.setFolderId(strFolderID);
/* 135 */     fileBo.setUserId(strUserID);
/* 136 */     fileBo.setFilePath(filePathBuffer.toString());
/* 137 */     fileBo.setUploadTime(new Date());
/* 138 */     fileBo.setFileSize(size / 1024L);
/* 139 */     fileBo.setIsShare("true");
/* 140 */     fileBo.setShareBeginTime(new Date());
/* 141 */     this.fileDao.saveFile(fileBo);
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 149 */     log.info("开始上传文件");
/* 150 */     String strReturn = "{success:true,msg:'文件上传成功！'}";
/*     */     try
/*     */     {
/* 153 */       DiskFileItemFactory factory = new DiskFileItemFactory();
/* 154 */       ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
/*     */ 
/* 158 */       createTempFolder();
/* 159 */       factory.setRepository(this.tempPathFile);
/*     */ 
/* 161 */       sevletFileUpload.setHeaderEncoding("utf-8");
/* 162 */       List fileItems = sevletFileUpload.parseRequest(request);
/* 163 */       Iterator iter = fileItems.iterator();
/* 164 */       String parentFolderID = null;
/* 165 */       String parentFolderName = null;
/* 166 */       String fileName = null;
/*     */ 
/* 168 */       while (iter.hasNext())
/*     */       {
/* 170 */         FileItem item = (FileItem)iter.next();
/* 171 */         String name = item.getName();
/* 172 */         if (item.isFormField()) {
/* 173 */           name = item.getFieldName();
/* 174 */           if ("strFolderParentID".equals(name))
/* 175 */             parentFolderID = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 176 */           if ("strFolderParentName".equals(name))
/* 177 */             parentFolderName = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 178 */           String value = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 179 */           log.info(name + ":" + value);
/*     */         }
/*     */       }
/* 182 */       log.info("*******[" + parentFolderID + ":" + parentFolderName + "]*****");
/* 183 */       String strFolderPath = getFilepPath(parentFolderID);
/* 184 */       log.info("strFolderPath : " + strFolderPath);
/*     */ 
/* 186 */       iter = fileItems.iterator();
/* 187 */       while (iter.hasNext())
/*     */       {
/* 189 */         FileItem item = (FileItem)iter.next();
/* 190 */         fileName = item.getName();
/* 191 */         if (!item.isFormField())
/*     */         {
/* 193 */           String strUserID = request.getSession().getAttribute("stxtUName").toString();
/* 194 */           double fileSize = item.getSize() / 1024L;
/*     */ 
/* 196 */           log.info("上传文件名是：[" + item + "]");
/* 197 */           if (fileName.lastIndexOf("\\") != -1)
/* 198 */             fileName = fileName.substring(fileName.lastIndexOf("\\"));
/* 199 */           File fullFile = new File(fileName);
/* 200 */           File savedFile = new File(strFolderPath, fullFile.getName());
/* 201 */           item.write(savedFile);
/*     */ 
/* 204 */           saveFile(fileName, parentFolderID, strUserID, savedFile.getAbsolutePath(), savedFile.length());
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileUploadException eup)
/*     */     {
/* 212 */       eup.printStackTrace();
/* 213 */       log.error(eup);
/*     */     }
/*     */     catch (SocketException clietEx) {
/* 216 */       log.warn("客户取消下载");
/*     */     }
/*     */     catch (Exception e) {
/* 219 */       strReturn = "{success:false,msg:'文件上传失败！'}";
/* 220 */       e.printStackTrace();
/*     */     }
/* 222 */     log.info(strReturn);
/* 223 */     response.setContentType("text/html; charset=utf-8");
/* 224 */     response.getWriter().print(strReturn);
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 230 */     doPost(request, response);
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.upload.UploadCommServlet
 * JD-Core Version:    0.6.2
 */